package com.x.example.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.x.example.PageRouteList;
import com.example.uiframework.utils.Utils;
import com.x.uikit.page.XPage;
import com.x.uikit.router.XNavigator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainPage extends XPage {
    private ArrayList<Map<String, Object>> mDemos = new ArrayList<>();

    public MainPage(@NonNull Context context) {
        super(context);
        setFitsSystemWindows(true);
        RecyclerView demosView = new RecyclerView(context);
        demosView.setLayoutManager(new LinearLayoutManager(context));
        demosView.setAdapter(createAdapter());
        this.addView(demosView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        initDemos();
        XNavigator.with(PageRouteList.NATIVE_MULTI_CHANNEL).navigate();
    }

    private void initDemos() {
        registerDemo("底部Tab", PageRouteList.NATIVE_BOTTOM_TAB);
        registerDemo("文章详情", PageRouteList.NATIVE_ARTICLE_TOOLBAR);
        registerDemo("TEST", PageRouteList.NATIVE_TEST);
        registerDemo("WEB", PageRouteList.NATIVE_WEB);
        registerDemo("多频道", PageRouteList.NATIVE_MULTI_CHANNEL);
    }

    private void registerDemo(String title, String path) {
        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("path", path);
        mDemos.add(map);
    }


    private RecyclerView.Adapter createAdapter() {
        return new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                TextView textView = new TextView(parent.getContext());
                textView.setMinHeight(Utils.res.dpI(50));
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setPadding(Utils.res.dpI(15), 0, 0, 0);
                RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                textView.setLayoutParams(lp);
                textView.setBackground(new ColorDrawable() {
                    Paint paint = new Paint();
                    int strokeWidth = Utils.res.dpI(1);
                    int lrOffset = Utils.res.dpI(10);

                    {
                        paint.setAntiAlias(true);
                        paint.setColor(0xffdfdfdf);
                        paint.setStyle(Paint.Style.STROKE);
                        paint.setStrokeWidth(strokeWidth);
                    }

                    @Override
                    public void draw(Canvas canvas) {
                        super.draw(canvas);
                        canvas.drawLine(lrOffset, getBounds().bottom - strokeWidth, getBounds().right - lrOffset, getBounds().bottom - strokeWidth, paint);
                    }
                });
                textView.setTextColor(0xff333333);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(textView) {
                };
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                TextView textView = (TextView) holder.itemView;
                Map<String, Object> map = mDemos.get(position);
                textView.setText((String) map.get("title"));
                final String url = (String) map.get("path");
                textView.setOnClickListener(v -> {
                    XNavigator.navigate(url);
                });

            }

            @Override
            public int getItemCount() {
                return mDemos.size();
            }
        };
    }
}